package com.jxinternet.platform.services.service.impl;

import com.jxinternet.platform.services.entity.domian.Fish;
import com.jxinternet.platform.services.entity.domian.Message;
import com.jxinternet.platform.services.entity.domian.User;
import com.jxinternet.platform.services.entity.domian.Wealth;
import com.jxinternet.platform.services.entity.protobuf.WealthProtos.WealthResponse;
import com.jxinternet.platform.services.protocol.BaseProtocol;
import com.jxinternet.platform.services.service.UserInfoService;
import com.jxinternet.platform.services.service.impl.steps.UserInfoSteps;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by gold-jx on 2017/7/19.
 */
@Slf4j
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoSteps userInfoSteps;

    private Message message;




    @Override
    public WealthResponse initUserInfo(String userName, String loginIp) {
        WealthResponse.Builder builder = WealthResponse.newBuilder();
        builder.setPoints(0);
        //log.info("开始加载财富："+"userName=["+userName+"]");
        try {
            //1.验证请求
            message=userInfoSteps.step1_validateRequest(userName);

            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            //2.获取用户
            message=userInfoSteps.step2_getUser(userName);

            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            User user = (User) message.getData();

            //3.获取财富
            message=userInfoSteps.step3_getWealth(userName);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            Wealth wealth= (Wealth) message.getData();

            //4.获取鱼类
            message=userInfoSteps.step4_getFish();
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            List<Fish> fishList= (List<Fish>) message.getData();

            //5.加载缓存
            message=userInfoSteps.step5_cache(user,wealth,fishList);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            //6.生成response
            builder.setPoints(wealth.getRechargePoint())
                    .setResultStatus(BaseProtocol.Data_Succeed_Code)
                    .setResultDescription(BaseProtocol.Data_Succeed_Des);
            //log.info("结束加载财富");

            //7.保存日志
            message=userInfoSteps.step6_saveLog(userName,loginIp);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            return builder.build();

        } catch (Exception e) {
            builder.setResultStatus(BaseProtocol.User_InvalidExcption_Code);
            builder.setResultDescription(BaseProtocol.User_InvalidExcption_Des);
            //log.error("加载财富失败",e);
            return builder.build();

        }
    }
}
